<?php

error_reporting(E_ALL);

/**
 * cn.solor.dig - class.BasicAnalyzer.php
 *
 * $Id: class.BasicAnalyzer.php,v 1.2 2008-01-14 15:32:04 rainx Exp $
 *
 * This file is part of cn.solor.dig.
 *
 * Automatic generated with ArgoUML 0.22 on 17.12.2006, 10:07:15
 *
 * @author firstname and lastname of author, <author@example.org>
 */

if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

/**
 * include Analyzer
 *
 * @author firstname and lastname of author, <author@example.org>
 */
// require_once('interface.Analyzer.php');

/* user defined includes */
// section 127-0-0-1--5e4d4b42:10f8dcad467:-8000:000000000000080C-includes begin
// section 127-0-0-1--5e4d4b42:10f8dcad467:-8000:000000000000080C-includes end

/* user defined constants */
// section 127-0-0-1--5e4d4b42:10f8dcad467:-8000:000000000000080C-constants begin
// section 127-0-0-1--5e4d4b42:10f8dcad467:-8000:000000000000080C-constants end

/**
 * Short description of class BasicAnalyzer
 *
 * @access public
 * @author firstname and lastname of author, <author@example.org>
 */
class BasicAnalyzer
        implements Analyzer
{
    // --- ATTRIBUTES ---

    /**
     * Short description of attribute features
     *
     * @access public
     * @var array
     */
    public $features = array();

    public $diggers = array();

    public $optDiggers = array();

    protected $url = '';
    protected $content = '';

    // --- OPERATIONS ---

    /**
     * Short description of method get
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param string
     * @param string
     * @return array
     */
    public function get($content, $url)
    {
        $this->init($content, $url);
        $res = $this->analyze($content, $url);
        $returnValue = sr_array_unique(array_merge($res, $this->runDiggers()));
        return (array) $returnValue;
    }

    /**
     * Short description of method getFeatures
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return array
     */
    public function getFeatures()
    {

        return (array) $this->features;
    }

    /**
     * Short description of method reg
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param Analyzer
     * @return int
     */
    public function reg( Analyzer $digger, $opt = false)
    {
        // get digger features
        if (!$opt)
        {
            if (!in_array($digger, $this->diggers))
            {
                $this->diggers[] = $digger;
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            if (!in_array($digger, $this->optDiggers))
            {
                $this->optDiggers[] = $digger;
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }

    /**
     * Short description of method check
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param Analyzer
     * @return boolean
     */
    public function check( Analyzer $analyzer)
    {
        $returnValue    = (bool) false;
        
        $features       = $analyzer->getFeatures();
        foreach ($features as $who => $reg)
        {
            if ($who == 'url')
            {
                if (preg_match($reg, $this->url) != 0) return true;
            }
            elseif($who == 'content')
            {
                if (preg_match($reg, $this->content) != 0) return true;
            }
        }
        
        return (bool) $returnValue;
    }


    /// Initialize the variable
    protected function init($content, $url)
    {
        $this->content = $content;
        $this->url     = $url;
    }

    /// run all Analyzers one by one

    function runDiggers()
    {
        $result = array();
        foreach ( $this->diggers as $digger)
        {
            if ($this->check($digger))
            {
                $ret = $digger->get($this->content, $this->url);
                $result = array_merge($result, $ret);
            } 
        }

        if (empty($result))
        {
            foreach ( $this->optDiggers as $digger)
            {
                if ($this->check($digger))
                {
                    $ret = $digger->get($this->content, $this->url);
                    $result = array_merge($result, $ret);
                }
            }

        }
        return sr_array_unique($result);
    }

    function analyze($content, $url = '')
    {
    }

    function getPage($url)
    {
        $req = new HTTP_Request($url);
        $req->addHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en_US; rv:1.8.0.8) Gecko/20061115 Ubuntu/dapper-security Firefox/1.5.0.8");
        $req->sendRequest();
        $body = $req->getResponseBody();
        return $body;
    }

} /* end of class BasicAnalyzer */
?>
